package com.shxc.smarttrade.kvb.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.base.BaseFragment;
import com.shxc.smarttrade.info.Contents;
import com.shxc.smarttrade.kvb.activity.KvbOpenAccountActivity;
import com.shxc.smarttrade.kvb.adapter.QuestionsAdapter;
import com.shxc.smarttrade.kvb.model.QuestionsModel;
import com.shxc.smarttrade.utils.analysis.JsonAnalysisModel;
import com.shxc.smarttrade.utils.analysis.JsonAnalysisUtils;
import com.shxc.smarttrade.utils.Loger;
import com.shxc.smarttrade.utils.MyOkHttpUtils;
import com.shxc.smarttrade.utils.ToastUtils;

import org.xchris.view.annotation.ContentView;
import org.xchris.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * KVB开户 二.问卷调查
 */
@ContentView(R.layout.recycler)
public class KvbOpen2Fragment extends BaseFragment {

    @ViewInject(R.id.recyclerview)
    RecyclerView recyclerview;

    private KvbOpenAccountActivity kvbOpenAccountActivity;
    private QuestionsAdapter adapter;
    private Button next_button;

    List<QuestionsModel.DataBean> list;
    private boolean isOver = false;


    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    QuestionsModel.DataBean.AnswersListBean am = (QuestionsModel.DataBean.AnswersListBean) msg.obj;
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).getId() == am.getQid()) {
                            list.get(i).setAid(am.getId());
                            break;
                        }
                    }
                    checkOver();

                    break;

            }
            super.dispatchMessage(msg);
        }
    };

    private void checkOver() {
        isOver = true;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getAid() == 0) {
                isOver = false;
                break;
            }
        }
        if (isOver) {
            next_button.setEnabled(true);
            next_button.setBackgroundResource(R.drawable.selector_bg_kvb);
        } else {
            next_button.setEnabled(false);
            next_button.setBackgroundColor(resources.getColor(R.color.kvb_unclick));
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        kvbOpenAccountActivity = (KvbOpenAccountActivity) getActivity();

        recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new QuestionsAdapter(R.layout.item_kvb_question, handler);

        View footview = getLayoutInflater().inflate(R.layout.footview_kvb, null);
        next_button = footview.findViewById(R.id.next_button);
        next_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                kvbOpenAccountActivity.makeProgress(3);

                upData();
            }
        });
        adapter.setFooterView(footview);
        recyclerview.setAdapter(adapter);
        initData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            checkOver();
        }
    }

    //获取题目数据
    private void initData() {
        loadingDialog.show();
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", "d31303049b7224712b2073354d4cc92c8a196f1894b1876509d78b4cd9268149b9ab165da9a31a6251d2448261fd99d1a7d22133bb3ef018493849f4a7896993");
        MyOkHttpUtils.getServiceDatas(map, Contents.KVB_QUESTIONS, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void success(String response, int d) {
                if (response != null) {
                    JsonAnalysisModel jam = JsonAnalysisUtils.jsonAnalysis(response, QuestionsModel.class);
                    if (jam.isSuccess()) {
                        QuestionsModel model = (QuestionsModel) jam.getData();
                        list = model.getData();
                        adapter.setNewData(model.getData());
                    } else {
                        ToastUtils.showToast(jam.getMsg());
                    }
                }
                loadingDialog.dismiss();
            }

            @Override
            public void faild(Call call, Exception e, int id) {
                loadingDialog.dismiss();
            }
        });
    }

    //上传题库
    private void upData() {
        List listt = new ArrayList();
        for (int i = 0; i <list.size() ; i++) {
            Map<String,Integer> map = new HashMap<>();
            map.put("qid",list.get(i).getId());
            map.put("aid",list.get(i).getAid());
            listt.add(map);
        }
        String qsJson = new Gson().toJson(listt);

        Map<String, Object> map = new HashMap<>();
        map.put("userId", 100);
        map.put("qsJson", qsJson);
        map.put("accessToken", "d31303049b7224712b2073354d4cc92c8a196f1894b1876509d78b4cd9268149b9ab165da9a31a6251d2448261fd99d1a7d22133bb3ef018493849f4a7896993");
        MyOkHttpUtils.getServiceDatas(map, Contents.KVB_COLLECTANSWERS, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void success(String response, int d) {
                if (response != null) {
                    JsonAnalysisModel jam = JsonAnalysisUtils.jsonAnalysis(response, QuestionsModel.class);
                    if (jam.isSuccess()) {
                        Loger.e("答案上传成功","!");
                    } else {
                        ToastUtils.showToast(jam.getMsg());
                    }
                }
            }
            @Override
            public void faild(Call call, Exception e, int id) {
                loadingDialog.dismiss();
            }
        });
    }

}
